package com.cherrish.demo.algorithm;

import java.util.Random;

/**
 * @author cherrishccl
 * @time 2019-03-29 15:24
 * @name RandomNumber
 * @desc:
 * 已知random_m()随机数生成器的范围是[1, m] 求random_n()生成[1, n]范围的函数，m < n && n <= m *m
 * int random_n()
 * {
 *     int val = 0;
 *     int t;   //t为n的最大倍数，且满足t<m*m
 *     do
 *     {
 *         val = m * (random_m() - 1) + random_m();
 *     }while(val > t);
 *     return val;
 * }
 */
public class RandomNumber {
    private final static int rand7(){
        return new Random().nextInt(7) + 1;
    }

    private final static int rand10(){
        int x = 0;
        do {
            x = (rand7() - 1) * 7 + rand7();
        }while (x > 40);
        return x % 10 + 1;
    }
}
